package com.yjx.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjx.dao.UserMapper;
import com.yjx.entity.User;
import com.yjx.service.UserService;
import com.yjx.service.UserTokenService;
import com.yjx.util.PasswordUtil;
import com.yjx.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2023-04-15 16:30:22
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserTokenService userTokenService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer id) {
        return this.userMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        this.userMapper.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userMapper.update(user);
        return this.queryById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.userMapper.deleteById(id) > 0;
    }

    @Override
    public UserVo login(User user) {
        //验证登录
        //通过用户名查询用户对象
        UserVo userVo1 = userMapper.queryByLoginName(user.getLoginName());
        if (userVo1 == null){
            return null;
        }
        //通过MD5解析完成对比
        String formerPWD = PasswordUtil.getFormerPWD(user.getPassword(), userVo1.getSalt());
        user.setPassword(formerPWD);
        UserVo userVo = userMapper.login(user);
        if (userVo == null){
            return  null;
        }
        userVo = userTokenService.createUserLoginToken(userVo);
        return userVo;
    }

    @Override
    public User queryBuUserCode(String userCode) {
        return userMapper.queryByUserCode(userCode);
    }

    @Override
    public Integer createUsr(User user, String token) {
        Date date = new Date();
        Map<String, String> Md5User = PasswordUtil.getMD5PassWord(user.getPassword());
        //通过token获取用户信息·1
        String userJson = userTokenService.getUserByToken(token);
        JsonMapper jsonMapper = new JsonMapper();
        try {
            UserVo userVo = jsonMapper.readValue(userJson,UserVo.class);
            user.setModifyUser(userVo.getId());
            user.setCreateTime(date);
            user.setModifyTime(date);
            user.setPassword(Md5User.get("password"));
            user.setSalt(Md5User.get("salt"));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        user.setDepartmentId(1);
        System.out.println(user.toString());
        return userMapper.insert(user);
    }

    @Override
    public PageInfo<UserVo> queryUserByUserName(String userName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<UserVo> userVoList = userMapper.queryUserByUserName(userName);
        PageInfo<UserVo> pageInfo = new PageInfo<>(userVoList);
        return pageInfo;
    }


}

